﻿Ext.define("PSI.InPlan.MakePlanMainForm", {
    createToolButtons: function () {
        var me = this;
        var result = [];
        result.push("-");
        result.push({ text: "下达计划", iconCls: "psi-button-page_white_swoosh", handler: me.onMakePlan, scope: me });
        result.push("-");
        result.push({ text: "取消计划", iconCls: "psi-button-cancel", handler: me.onCancelPlan, scope: me });
        result.push("-");
        result.push({ text: "更改调度", iconCls: "psi-button-user_edit", handler: me.onChangeDispatcher, scope: me });

        result.push("-");
        result.push({ text: "刷新", iconCls: "psi-button-refresh", handler: me.onRefreshPlanGird, scope: me });
        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                location.replace("/");
            }
        });
        return result;
    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "下达进口计划",
            tbar: me.createToolButtons(),
            items: [{
                region: "center", xtype: "container", layout: "fit", border: false,
                items: [me.getInPlanGrid()]
            }, {
                region: "south", xtype: "container", border: false, height: 30,
                items: [
                    {
                        xtype: "displayfield",
                        id: "editPlanInfo",
                        value: ""
                    }
                ]
            }
            ]
        });

        me.panel = panel;
        me.refreshInPlanGrid();
    },

    getMainPanel: function () {
        return this.panel;
    },

    getInPlanGrid: function () {
        var me = this;

        if (!me.__inPlanGrid) {
            me.__inPlanGrid = me.createInPlanGrid();
        }

        return me.__inPlanGrid;
    },

    createInPlanGrid: function () {
        var modelName = "PSIInPlanModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "planNumber", "vsl", "clients", "address", "cntOwner", "cntNumber",
                    "cntType", "deliverDT", "memo", "planDT", "dispatcher", "status"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            columnLines: true,
            selModel: Ext.create("Ext.selection.CheckboxModel"),
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "状态", dataIndex: "status", menuDisabled: true },
                    { header: "进口计划号", dataIndex: "planNumber", menuDisabled: true },
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 230 },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true, width: 60 },
                    { header: "送货时限", dataIndex: "deliverDT", menuDisabled: true, width: 120 },
                    { header: "备注", dataIndex: "memo", menuDisabled: true },
                    { header: "计划下达时间", dataIndex: "planDT", menuDisabled: true, width: 120 },
                    { header: "调度", dataIndex: "dispatcher", menuDisabled: true }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    onRefreshPlanGird: function () {
        this.refreshInPlanGrid();
    },

    refreshInPlanGrid: function (selectedId) {
        var me = this;
        var grid = me.getInPlanGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }

        var params = {
        };

        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/InPlan/GetMakePlanListData",
            params: params,
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                    var info = "进口计划共计: <span style='color:red'>" + data.length + "</span>个";
                    Ext.getCmp("editPlanInfo").setValue(info);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onMakePlan: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length == 0) {
            PSI.MsgBox.showInfo("请选择要下达的计划");
            return;
        }

        // 检查该计划是否已经下达过了
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            if (obj.planDT) {
                var info = "计划号为[" + obj.planNumber + "]的进口计划已经下达";
                PSI.MsgBox.showInfo(info);
                return;
            }
        }

        var idList = [];
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            idList.push(obj.id);
        }

        var form = Ext.create("PSI.InPlan.MakePlanForm");
        form.show(me, { idList: Ext.JSON.encode(idList) }, items);
    },

    onCancelPlan: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要一个取消的计划");
            return;
        }

        var plan = items[0].getData();
        if (plan.status === "待下达计划") {
            PSI.MsgBox.showInfo("当前计划还没有下达，无需取消");
            return;
        }

        if (plan.status === "已派车") {
            var info = "当前计划已经派车，不能取消";
            PSI.MsgBox.showInfo(info);
            return;
        }

        PSI.MsgBox.confirm("请确认是否取消计划[" + plan.planNumber + "]", function () {
            var el = Ext.getBody();
            el.mask("正在处理中...");
            Ext.Ajax.request({
                url: "/InPlan/CancelPlan",
                method: "POST",
                params: { id: plan.id },
                callback: function (options, success, response) {
                    el.unmask();

                    if (success) {
                        var data = Ext.JSON.decode(response.responseText);
                        if (data.success) {
                            PSI.MsgBox.showInfo("成功取消计划", function () {
                                me.refreshInPlanGrid();
                            });
                        } else {
                            PSI.MsgBox.showInfo(data.msg);
                        }
                    } else {
                        PSI.MsgBox.showInfo("网络错误");
                    }
                }
            });
        });
    },

    onChangeDispatcher: function () {
        var me = this;
        var items = me.getInPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length == 0) {
            PSI.MsgBox.showInfo("请选择要更改调度的计划");
            return;
        }

        // 检查计划是否都已经下达
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            if (!obj.planDT) {
                var info = "计划没有下达，不能更改调度";
                PSI.MsgBox.showInfo(info);
                return;
            }
        }

        var idList = [];
        for (var i = 0; i < items.length; i++) {
            var obj = items[i].getData();
            idList.push(obj.id);
        }

        var form = Ext.create("PSI.InPlan.ChangeDispatcherForm");
        form.show(me, { idList: Ext.JSON.encode(idList) });
    }

});